package Tree;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-30  08:35
 * @Description: TODO
 * @Version: 1.0
 */
public class N叉树的最大深度559 {

    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        // 记录子树中最大的那个
        // 左右都已经计算好了
        int value = 0;
        for (Node node : root.children) {
            int depth = maxDepth(node);
            value = Math.max(value, depth);
        }
        // 中间的
        int mid = value + 1;
        return mid;
    }

    // 使用的是层序遍历的方法
    public int maxDepth2(Node root) {
        if (root == null) {
            return 0;
        }
        Deque<Node> deque = new LinkedList<>();
        deque.offer(root);
        int depth = 0;
        while (!deque.isEmpty()) {
            int size = deque.size();
            depth++;
            for (int i = 0; i < size; i++) {
                Node node = deque.poll();
                // 左右
                for (Node node1 : node.children) {
                    if (node1 != null) {
                        deque.add(node1);
                    }
                }
            }
        }
        return depth;
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
}
